/* execbasic.c contains basic functions the executable should have, param handling, quitting... */

#include "execbasic.hxx"

void handleArgs(char argN, char *argT[])
{
	char argC = 0;
	
	while (argC < argN)
	{
		if (strcmp(argT[argC],"-nc") == 0)
		{
			scro.saveSettings = FALSE;
			logger.log(0, "settings will NOT be saved.");
		}
	
		if (strcmp(argT[argC],"-f") == 0)
		{
			scro.settings.flags |= SDL_FULLSCREEN;
			logger.log(0, "will start in fullscreen mode...");
		}

		if (strcmp(argT[argC],"-b8") == 0)
		{
			scro.settings.bpp = 8;
			logger.log(0, "will start in 8bit color mode...");
		}

		if (strcmp(argT[argC],"-b16") == 0)
		{
			scro.settings.bpp = 16;
			logger.log(0, "will start in 16bit color mode...");
		}

		if (strcmp(argT[argC],"-b24") == 0)
		{
			scro.settings.bpp = 24;
			logger.log(0, "will start in 24bit color mode...");
		}
		
		if (strcmp(argT[argC], "--level") == 0)
		{
			if (argT[argC + 1] == NULL)
			{
				logger.log(1, "level file not specified, leaving default.");
			} else
			{
				logger.log(0, "will try level file '%s", argT[argC+1]);
				sprintf(scro.levelfile, "%s", argT[argC+1]);
			}
		}

		/*if (strcmp(argT[argC],"-ns") == 0)
		{
			pongian.settings.soundEnabled = 0;
			logger.log(0, "sound support disabled.");
		}*/


		if (strcmp(argT[argC],"--help") == 0)
		{
			printf("---------------------------------------------------------------------------\n");
			printf(" scro command line options....\n");
			printf(" scro [options]\n");
			printf(" options are: -f fullscreen Mode\n");
			printf("              -b[8|16|24] start in 8/16/24bit color mode (default: %d)\n", VIDEO_BITS);
			printf("              -t [dir] where dir is the theme directory\n");
			printf("              -nc do not save settings at end\n");
			printf("			  --level [level file name] use specified level file.\n");
			printf("---------------------------------------------------------------------------\n");
			printf("scro version %s.\n", SCRO_VERSION);
			quitScro(SCRO_CLI_EXIT);
		}
		
		argC++;
	}
}


void quitScro(char mode)
{
	if (scro.saveSettings) saveSettings();
	switch(mode)
	{
		case SCRO_CLI_EXIT:
		break;

		case SLI_ERROR_LOADFAILURE:
			logger.log(2, "allocating memory for level failed!");
		break;

		case SLI_ERROR_INVALIDLEVEL:
			logger.log(2, "level file '%s' is invalid!", scro.levelfile);
		break;
	
		case SCRO_ERROR_TILES:
			logger.log(2, "error loading level tiles!");
		break;
	
		default:
		break;
	}
	
	if (mode != SCRO_CLI_EXIT)
	{
		SDL_FreeSurface(screen);
		//SDL_FreeSurface(scro.iconPic);
		SDL_FreeSurface(fire);
		SDL_FreeSurface(badguy);
		SDL_Quit();
	}	
	
	logger.log(0, "exiting, good bye!");
	logger.end();
	delete [] scro.execPath;
	delete [] scro.configFile;
	delete [] scro.tempChar;
	exit(mode);
}

